package ims.servlet;

import ims.beans.Product;
import ims.beans.ProductType;
import ims.constants.IMSConstants;
import ims.db.add.product.AddProductDAO;
import ims.db.add.product.AddProductDbRequest;
import ims.db.add.product.AddProductDbResponse;
import ims.db.view.prodType.ViewProdTypesDAO;
import ims.db.view.prodType.ViewProdTypesDbRequest;
import ims.db.view.prodType.ViewProdTypesDbResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AddProductServlet
 */
@WebServlet("/AddProductServlet")
public class AddProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try
		{
			String newProd = request.getParameter("prodNm");
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
	        
	        //generating output of page
			out.println("<html>");
            out.println("<head>");
            out.println("<title>Add Product</title>");
            out.println("</head>");
            out.println("<body>");
            
            out.println("<form name='/IMS/addProductForm' action='/IMS/addProduct' method='post'>" );
            out.println("Product: <input type='text' name='prodNm' readonly='readonly' value='" + newProd + "' /></br>");
            
            //Retrieve all product types
			ViewProdTypesDbRequest dbRequest = new ViewProdTypesDbRequest();
			dbRequest.setViewAll(true);
			ViewProdTypesDAO db = new ViewProdTypesDAO();
			ViewProdTypesDbResponse dbResponse = (ViewProdTypesDbResponse) db.execute(dbRequest);
			ArrayList<ProductType>prodTypes = dbResponse.getProdTypes();
			//Product Type field
			out.println("Product Type: <select name='prodType'>");
            for(int i=0; i<prodTypes.size();i++)
            {
            	ProductType prodType = prodTypes.get(i);
            	out.println("<option value='" +prodType.getProdTypeId()+"'>" + prodType.getProdType()+"</option>" );
            }
            out.println("</select> </br>");
            out.println("Price: <input type='text' name='price' /></br>");
            out.println("<input type='submit' value='Add Product' />");
            out.println(" </form> " );
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			Product product = new Product();
			product.setPid("P0000"+IMSConstants.PROD_ID);
			product.setProdDesc(request.getParameter("prodNm"));
			product.setPrice(Double.parseDouble(request.getParameter("price")));
			product.setProdTypeId(request.getParameter("prodType"));
			AddProductDbRequest dbRequest = new AddProductDbRequest();
			dbRequest.setProduct(product);
			AddProductDAO db = new AddProductDAO();
			AddProductDbResponse dbResponse = (AddProductDbResponse)db.execute(dbRequest);
			
			String nextLoc = "/IMS/product?"+IMSConstants.RESPONSE_HEADER+"="+dbResponse.getMsg();      
			response.sendRedirect(response.encodeRedirectURL(nextLoc));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
